package com.itic.hpp.collage;

import com.itic.appbase.applications.redis.IRedisDao;
import com.itic.appbase.framework.Base.BaseCode;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mobile.base.BaseMobile;
import com.itic.system.collage.category.persistence.model.CollageCategory;
import com.itic.system.collage.category.service.CollageCategoryService;
import com.itic.system.collage.collage.persistence.model.Collage;
import com.itic.system.collage.collage.persistence.model.CollageExample;
import com.itic.system.collage.collage.service.CollageService;
import com.itic.system.collage.imageVideo.persistence.model.CollageImageVideo;
import com.itic.system.collage.imageVideo.service.CollageImageVideoService;
import com.itic.system.collage.sku.persistence.model.CollageSku;
import com.itic.system.collage.sku.service.CollageSkuService;
import com.itic.system.collage.skuProperty.persistence.model.CollageSkuProperty;
import com.itic.system.collage.skuProperty.service.CollageSkuPropertyService;
import com.itic.system.order.order.persistence.model.OrderExample;
import com.itic.system.order.order.service.OrderService;
import com.itic.system.spu.category.persistence.model.SpuCategory;
import com.itic.system.spu.category.service.SpuCategoryService;
import com.itic.system.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping(value = "/hpp/collage")
public class CollageInterface extends BaseMobile {

    @Autowired
    private CollageService collageService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private CollageSkuService collageSkuService;
    @Autowired
    private CollageCategoryService collageCategoryService;
    @Autowired
    private SpuCategoryService spuCategoryService;
    @Autowired
    private CollageImageVideoService collageImageVideoService;
    @Autowired
    private IRedisDao<String,Object> redisDao;

    /**
     * 提交拼团
     * @param request
     * @param response
     * @param collageVo
     * @return
     */
    @RequestMapping(value="/submit")
    @ResponseBody
    public Map<String,Object> submitCollage(HttpServletRequest request, HttpServletResponse response,@RequestBody  CollageVo collageVo){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        String errorCode = PARAM_ERROR_CODE;
        String errorMsg = PARAM_ERROR_MSG;
        try{
            if(collageVo!=null && StringUtils.isNoneBlank(collageVo.getCollageType(),collageVo.getDeliveryMethod(),collageVo.getPayMethod(),collageVo.getUserId()) && collageVo.getOriginalPrice()!=null && collageVo.getCollagePriceVo()!=null && !collageVo.getCollagePriceVo().isEmpty() && collageVo.getCollageTime()!=null &&  collageVo.getTotalInventorys()!=null){
                resultMap = this.collageService.submit(collageVo);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "submitCollage", e.toString(),"发布拼团",e);
        }
        return resultMap;
    }

    /**
     * 发布拼团下一步保存基本信息
     * @param request
     * @param response
     * @param nextStepVo
     * @return
     */
    @RequestMapping(value="/nextStep")
    @ResponseBody
    public Map<String,Object> nextStep(HttpServletRequest request, HttpServletResponse response, @RequestBody NextStepVo nextStepVo){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(nextStepVo!=null && StringUtils.isNoneBlank(nextStepVo.getUserId(),nextStepVo.getTitle(),nextStepVo.getDescript(),nextStepVo.getLocalAddress()) && nextStepVo.getImageVieos()!=null &&  !nextStepVo.getImageVieos().isEmpty())
                //处理拼团下一步保存业务逻辑
                resultMap = this.collageService.nextStep(nextStepVo.getUserId(),nextStepVo.getCollageId(),nextStepVo.getTitle(),nextStepVo.getDescript(),nextStepVo.getImageVieos(),nextStepVo.getLocalAddress());
            else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "nextStep", e.toString(),"发布拼团下一步保存",e);
        }
        return resultMap;
    }

    /**
     * 添加或修改商品属性类型 属性值名称
     * @param request
     * @param response
     * @param userId
     * @param spuCategoryId
     * @param skuNames
     * @param collageId
     * @param collageCategoryId
     * @return
     */
    @RequestMapping(value="/addOrEditSku")
    @ResponseBody
    public Map<String,Object> addOrEditSku(HttpServletRequest request, HttpServletResponse response,String userId,String spuCategoryId,String skuNames,String collageId,String collageCategoryId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNoneBlank(userId,spuCategoryId,skuNames,collageId)){
                resultMap = this.collageService.addOrEditSku(userId,spuCategoryId,skuNames,collageId,collageCategoryId);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "addOrEditSku", e.toString(),"编辑或者保存规格类型和名称",e);
        }
        return resultMap;
    }

    /**
     * 拼团列表
     * @param request
     * @param response
     * @param userId
     * @return
     */
    @RequestMapping(value="/list")
    @ResponseBody
    public Map<String,Object> list(HttpServletRequest request, HttpServletResponse response,String userId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNotEmpty(userId)){
                CollageExample collageExample = new CollageExample();
                this.handlePageQueryDefault(request,collageExample);
                resultMap = this.collageService.collageList(collageExample,userId);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "addOrEditSku", e.toString(),"编辑或者保存规格类型和名称",e);
        }
        return resultMap;
    }

    /**
     * 拼团详情页
     * @param request
     * @param response
     * @param collageId
     * @return
     */
    @RequestMapping(value="/detail")
    @ResponseBody
    public Map<String,Object> detail(HttpServletRequest request, HttpServletResponse response,String collageId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNotEmpty(collageId)){
                //拼团详情页
                resultMap = this.collageService.collageDetail(collageId);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "detail", e.toString(),"拼团详情页",e);
        }
        return resultMap;
    }

    /**
     * 我发不的团 分页
     * @param request
     * @param response
     * @param userId
     * @return
     */
    @RequestMapping(value="/myPublishCollage")
    @ResponseBody
    public Map<String,Object> myPublishCollage(HttpServletRequest request, HttpServletResponse response,String userId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNotEmpty(userId)){
               CollageExample example = new CollageExample();
               this.handlePageQueryDefault(request,example);
               resultMap = this.collageService.myPublishCollage(example,userId);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "myPublishCollage", e.toString(),"我发布的团",e);
        }
        return resultMap;
    }

    /**
     * 我参加的团
     * @param request
     * @param response
     * @param userId
     * @return
     */
    @RequestMapping(value="/myJoinCollage")
    @ResponseBody
    public Map<String,Object> myJoinCollage(HttpServletRequest request, HttpServletResponse response,String userId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNotEmpty(userId)){
                OrderExample example = new OrderExample();
                this.handlePageQueryDefault(request,example);
                resultMap = this.orderService.myJoinCollage(example,userId);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "myJoinCollage", e.toString(),"我参加的团",e);
        }
        return resultMap;
    }

    /**
     * 高级设置详情页数据
     * @param request
     * @param response
     * @param collageId
     * @return
     */
    @RequestMapping(value="/gjSkuDetailPage")
    @ResponseBody
    public Map<String,Object> gjSkuDetailPage(HttpServletRequest request, HttpServletResponse response,String collageId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNotEmpty(collageId))
                resultMap =  this.collageService.getGJSkuDetailVoByCollageId(collageId);
            else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "skuDetail", e.toString(),"高级设置详情页数据",e);
        }
        return resultMap;
    }

    /**
     * 根据当前拼团类型Id 查看属性类型 已经属性值
     * @param request
     * @param response
     * @param collageCategoryId
     * @return
     */
    @RequestMapping(value="/skuDetailPage")
    @ResponseBody
    public Map<String,Object> skuDetailPage(HttpServletRequest request, HttpServletResponse response,String collageCategoryId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNotEmpty(collageCategoryId)){
                List<CollageSku> collageSkus =  this.collageSkuService.findCollageSkuByCategoryId(collageCategoryId);
                Map<String,Object> result = new HashMap<>();
                CollageCategory category  = this.collageCategoryService.get(collageCategoryId);
                SpuCategory spuCategory = this.spuCategoryService.get(category.getCategoryId());
                result.put("spuCategoryId",spuCategory.getId());
                result.put("spuCategoryName",spuCategory.getName());
                result.put("collageCategoryId",collageCategoryId);
                List<Map<String,Object>>  skuNames = new ArrayList<>();
                for(CollageSku collageSku : collageSkus){
                    Map<String,Object> sku = new HashMap<>();
                    sku.put("name",collageSku.getSkuName());
                    skuNames.add(sku);
                }
                result.put("skuNames",skuNames);
                resultMap.put(BaseCode.DATA, result);
                resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
             }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "skuDetailPage", e.toString(),"规格页面详情",e);
        }
        return resultMap;
    }

    /**
     * 匹配规格并返回规格Property详情
     * @param request
     * @param response
     * @param collageId
     * @return
     */
    @RequestMapping(value="/skuPropertyDetail")
    @ResponseBody
    public Map<String,Object> skuPropertyDetail(HttpServletRequest request, HttpServletResponse response,String collageId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNotEmpty(collageId)){
                resultMap = this.collageService.getSkuPropertyDetail(collageId);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "skuPropertyDetail", e.toString(),"规格匹配详情页",e);
        }
        return resultMap;
    }


    /**
     * 保存规格匹配库存
     * @param request
     * @param response
     * @param collageSkuVos
     * @return
     */
    @RequestMapping(value="/saveInventorys")
    @ResponseBody
    public Map<String,Object> saveInventorys(HttpServletRequest request, HttpServletResponse response, @RequestBody  List<CollageSkuVo> collageSkuVos){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(!collageSkuVos.isEmpty()){
                this.collageService.saveInventorys(collageSkuVos);
                resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "saveInventorys", e.toString(),"保存库存接口",e);
        }
        return resultMap;
    }

    /**
     * 删除拼团属性
     * @param request
     * @param response
     * @param collageId 拼团Id
     * @param collageCategoryId 拼团属性Id
     * @return
     */
    @RequestMapping(value="/delCollageSku")
    @ResponseBody
    public Map<String,Object> delCollageSku(HttpServletRequest request, HttpServletResponse response, String collageId,String collageCategoryId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNoneBlank(collageId,collageCategoryId)){
                resultMap = this.collageService.delCollageSkuCategory(collageId,collageCategoryId);
                resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "delCollageSku", e.toString(),"删除拼团属性接口",e);
        }
        return resultMap;
    }

    /**
     * 下一步详情
     * @param request
     * @param response
     * @param collageId
     * @return
     */
    @RequestMapping(value="/nextStepDetail")
    @ResponseBody
    public Map<String,Object> nextStepDetail(HttpServletRequest request, HttpServletResponse response, String collageId){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNoneBlank(collageId)){
                Collage collage = this.collageService.get(collageId);
                if(collage!=null){
                    Map<String, Object> data = new HashMap<String,Object>();
                    data.put("title",collage.getTitle());
                    data.put("localAddress",collage.getLocaltionAddress());
                    data.put("descript",collage.getDescription());
                    List<CollageImageVideo> collageImageVideos = this.collageImageVideoService.findCollageImageVideoByCollageId(collageId);
                    List<Map<String, Object>> list = new ArrayList<>();
                    for(CollageImageVideo collageImageVideo : collageImageVideos){
                        Map<String, Object> imageData = new HashMap<String,Object>();
                        imageData.put("type",collageImageVideo.getType());
                        imageData.put("url",collageImageVideo.getUrl());
                        list.add(imageData);
                    }
                    data.put("imageVideoVos",list);
                    resultMap.put(BaseCode.DATA,data);
                    resultMap.put(BaseCode.CODE, BaseCode.SUCCESS_ERROR_CODE);
                    resultMap.put(BaseCode.MSG, BaseCode.SUCCESS_ERROR_MSG);
                }else{
                    resultMap.put(BaseCode.CODE, BaseCode.HPP_COLLAGE_NOT_EXIST_CODE);
                    resultMap.put(BaseCode.MSG, BaseCode.HPP_COLLAGE_NOT_EXIST_MSG);
                }
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "delCollageSku", e.toString(),"删除拼团属性接口",e);
        }
        return resultMap;
    }

    /**
     * 分享拼团生成图片
     * @param request
     * @param response
     * @param collageId
     * @param url
     * @return
     */
    @RequestMapping(value="/shareImage")
    @ResponseBody
    public Map<String,Object> shareImage(HttpServletRequest request, HttpServletResponse response, String collageId,String url){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            if(StringUtils.isNoneBlank(collageId,url)){
                resultMap = this.collageService.shareImage(collageId,url);
            }else{
                resultMap.put(BaseCode.CODE, BaseCode.PARAM_ERROR_CODE);
                resultMap.put(BaseCode.MSG, BaseCode.PARAM_ERROR_MSG);
            }
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "shareImage", e.toString(),"分享拼团图片接口",e);
        }
        return resultMap;
    }

    /**
     * 直接到账  拼团结束多久给团长打款
     * @return
     */
    @RequestMapping(value="/directPayToCollageHead")
    @ResponseBody
    public Map<String,Object> directPayToCollageHead(Integer day){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        try{
            int outOfDay = day==null?7:day.intValue();
            this.collageService.directPayToCollageHead(outOfDay);
        }catch(Exception e){
            resultMap.put(BaseCode.CODE, BaseCode.SERVER_ERROR_CODE);
            resultMap.put(BaseCode.MSG, BaseCode.SERVER_ERROR_MSG);
            logException("CollageInterface", "directPayToCollageHead", e.toString(),"直接到账拼团打钱给团长接口",e);
        }
        return resultMap;
    }
    @RequestMapping(value="/redisTest")
    @ResponseBody
    public void redisTest(){
        this.redisDao.set("aaaabcdaaa","aaaaaa");
        this.redisDao.set("aaaabcdbbb","aaaaaa");
        this.redisDao.set("aaaabcdccc","aaaaaa");
        this.redisDao.set("aaaabcdddd","aaaaaa");
        this.redisDao.set("aaaaa","bbbbb");
        Set<String> keys = this.redisDao.keys("CollageBuyUserf5cb94d629fb4046a6efbe7bf5c03278*");
        System.out.println(keys.size());

    }

    @RequestMapping(value="/delredisTest")
    @ResponseBody
    public void delredisTest(){
        this.redisDao.delete("aaaaa");

    }
}
